package com.example.demo.ding;

import com.alibaba.fastjson.JSON;
import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.OapiGettokenRequest;
import com.dingtalk.api.request.OapiMediaUploadRequest;
import com.dingtalk.api.request.OapiMessageCorpconversationAsyncsendV2Request;
import com.dingtalk.api.response.OapiGettokenResponse;
import com.dingtalk.api.response.OapiMediaUploadResponse;
import com.dingtalk.api.response.OapiMessageCorpconversationAsyncsendV2Response;
import com.taobao.api.ApiException;
import com.taobao.api.FileItem;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * @Author: zjq
 * @Date: create in 2022/10/8 9:41
 */
@Api(tags = {"企业内部应用消息推送"})
@RestController
@RequestMapping("/ding")
public class mesController {

    @Value("${dingding.AppKey}")
    private String APPKEY;
    @Value("${dingding.AppSecret}")
    private String APPSECRET;
    @Value("${dingding.AgentId}")
    private Long AgentId;

    //按下面的步骤获取这两个值
    String access_token = "dc2f863d45263b06b35057c945faeb86";
    String media_id = "@lAjPDfYH9T4DEw_OKgkJJ85HChaT";


    @ApiOperation(value = "调试接口testutil")
    @PostMapping(value = "/testutil")
    public void testutil() throws ApiException {
        DingUtil.gettoken(APPKEY, APPSECRET);
        System.out.println(DingUtil.getUserid("19357611719"));
    }

    @ApiOperation(value = "直接使用DingUtil发送多个压缩包，单个文件钉钉限制20M")
    @PostMapping(value = "/sendFiles")
    public void sendFiles() {
        DingUtil.gettoken(APPKEY, APPSECRET);//获取token,每两小时失效

        for (int i = 1; i <= 5; i++) {
            String filename = "C:\\Users\\THINK\\Desktop\\新建文件夹\\mytest (1).pdf\\mytest (1)-001.zip.00" + i + "";
            System.out.println(filename);
            DingUtil.sendFile(filename);//向钉钉推送文件
            DingUtil.sendMessage(DingUtil.getUserid("19357611719"), AgentId);//单条消息
        }

    }


    //第三步
    @ApiOperation(value = "发送消息")
    @PostMapping(value = "/send")
    public void send() throws ApiException {
        DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/message/corpconversation/asyncsend_v2");
        OapiMessageCorpconversationAsyncsendV2Request request = new OapiMessageCorpconversationAsyncsendV2Request();
        request.setAgentId(1936838885L);//发送消息时使用的微应用的AgentID
        request.setUseridList("01423361270421425535");//接收者的userid列表
        request.setToAllUser(false);

        OapiMessageCorpconversationAsyncsendV2Request.Msg msg = new OapiMessageCorpconversationAsyncsendV2Request.Msg();

        msg.setMsgtype("file");
        msg.setFile(new OapiMessageCorpconversationAsyncsendV2Request.File());
        msg.getFile().setMediaId(media_id);
        request.setMsg(msg);

        OapiMessageCorpconversationAsyncsendV2Response rsp = client.execute(request, access_token);
        System.out.println(rsp.getBody());

        //{
        //    "errcode":0,
        //        "task_id":256271667526,
        //        "request_id":"4jzllmte0wau"
        //}
    }


    //第二步
    @ApiOperation(value = "向钉钉推送文件")
    @PostMapping(value = "/posy")
    public void importTeacherinfo() throws ApiException {
        DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/media/upload");
        OapiMediaUploadRequest req = new OapiMediaUploadRequest();
        req.setType("file");
        // 要上传的媒体文件
        FileItem item = new FileItem("C:\\Users\\THINK\\Desktop\\bj.txt");
        req.setMedia(item);
        OapiMediaUploadResponse rsp = client.execute(req, access_token);
        System.out.println(rsp.getBody());

        Map<String, Object> map = JSON.parseObject(rsp.getBody(), Map.class);
        System.out.println("map值为" + map);

        media_id = map.get("media_id").toString();
        System.out.println("media_id" + media_id);
        //{
        //    "errcode": 0,
        //        "errmsg": "ok",
        //        "media_id": "$iAEKAqNwbmcDBgTNAk",
        //        "created_at": 1605863153573,
        //        "type": "image"
        //}
    }


    //第一步
    @ApiOperation(value = "获取access_token")
    @GetMapping(value = "/getToken")
    public void importTeacherinfos() throws ApiException {
        DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/gettoken");
        OapiGettokenRequest request = new OapiGettokenRequest();
        request.setAppkey(APPKEY);
        request.setAppsecret(APPSECRET);
        request.setHttpMethod("GET");
        OapiGettokenResponse response = client.execute(request);
        System.out.println(response.getBody());

        Map<String, Object> map = JSON.parseObject(response.getBody(), Map.class);
        System.out.println("map值为" + map);

        access_token = map.get("access_token").toString();
        System.out.println("access_token是" + access_token);
        //{
        //    "errcode": 0,
        //    "access_token": "96fc7a7axxx",
        //    "errmsg": "ok",
        //    "expires_in": 7200
        //}
    }
}
